// pages/search/index.js
let timeId=''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDelete: false,
    showResults: false,
    noResult: false,
    showSuggestion: false,
    isFocus:false,
    searchInput: '',
    suggestion:[],
    resultsList: [],
    searchHistory:[],
  },

  onLoad() {
  this.getHistory()
  },

  getHistory() {
    const searchHistory = wx.getStorageSync('search-history')
  this.setData({
    searchHistory,
  })
  },

  cancelSearch() {
    wx.redirectTo({
      url:'/pages/index/index'
    })
    this.setData({
      resultsList: [],
      searchInput:'',
      showDelete: false,
      noResult: false
    })
  },

  async goSearch(ev) {
    if (ev.detail.trim() === '') return wx.utils.toast('请输入搜索内容')
    const {data:{data}} = await wx.http.get('/v1_0/search', { q: ev.detail })
    console.log('搜索结果列表', data.results)
    if (data.results.length === 0) {
      this.setData({
       noResult: true
      })
    }
    const key = parseInt(Math.random()*10000)
    const arr = [{ id: key, value: ev.detail }, ...this.data.searchHistory]
    const newHistory = []
    arr.forEach(item1 => {
      if (!newHistory.some(item2 => item2.value === item1.value)) {
        newHistory.push(item1)
      }
    })
    if (newHistory.length>8) newHistory.pop()
    wx.setStorageSync('search-history', newHistory)
    this.setData({
      resultsList: data.results,
      searchHistory: newHistory,
      showResults: true
    })
   },
  
   goDetailPage(ev) {
     wx.redirectTo({
       url:'/pages/article/index?id=' + ev.mark.id
     })
   },
 
   focusInput() {
     this.setData({
       showDelete: false,
       noResult: false,
       showSuggestion:true
     })
   },
 
   blurInput() {
     this.setData({
       suggestion:[],
       showSuggestion:false
     })
   },
 
   changeInput(ev) {
     if (ev.detail === '') {
       this.setData({
         resultsList:[]
       })
     }
     if (timeId) clearTimeout(timeId)
     timeId = setTimeout(() => {
       this.getSuggestion(ev.detail)
     },1000)
   },
 
   async getSuggestion(val) {
     if (val) {
       const {data:{data}} = await wx.http.get('/v1_0/suggestion', {q:val})
       console.log('搜索建议', data.options)
       this.setData({
         suggestion: data.options,
         showSuggestion: !!data.options[0]
       })
     }
   },
 
  inputSuggestion(ev) {
    this.setData({
      searchInput: ev.mark.value,
      isFocus:true,
      showSuggestion:false
    })
   },

   openDelete() {
     this.setData({
       showDelete: true,
     })
   },
 
   closeDelete() {
     this.setData({
       showDelete:false
     })
   },
 
  searchAgain(ev) {
     if(this.data.showDelete || this.data.searchHistory.length ===0) return
     const temp = this.data.searchHistory.find(item=>item.id===ev.mark.id).value
     this.setData({
       searchInput:temp
     })
   },
 
   deleteHistory(ev) {
     console.log('删除历史')
     const newHistory = this.data.searchHistory.filter(item => item.id !== ev.mark.id)
     if (newHistory.length === 0) {
       this.setData({
         showDelete: false
       })
     }
     wx.setStorageSync('search-history', newHistory)
     this.setData({
       searchHistory: newHistory
     })
   },
 
   deleteAll() {
     wx.removeStorageSync('search-history')
     this.setData({
       searchHistory: []
     })
   },
 
})